package com.yunhe.marketing.controller;

import com.yunhe.common.annotation.OperationRecord;
import com.yunhe.common.constant.SystemConsts;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.model.ResultObject;
import com.yunhe.marketing.domain.CompanyEvent;
import com.yunhe.marketing.service.ICompanyEventService;
import com.yunhe.marketing.util.IpAddressUtil;
import com.yunhe.marketing.util.MyHeaderUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * @Author: DongWenYu
 * @Date: 2021/3/12 18:02
 */
@Api(value = "companyEvent", description = "企业事件")
@RestController
@RequestMapping("/exposure")
public class CompanyEventController {

    @Autowired
    ICompanyEventService exposureService;

    @Autowired
    MyHeaderUtil headerUtil;


    @ApiOperation("发送曝光企业列表")
    @PostMapping("/sendExposureCompanyList")
    @OperationRecord(menu = "企业事件库管理", actionType = SystemConsts.ACTION.ADD)
    public ResponseEntity<ResultObject> sendExposureCompanyList(@RequestBody List<CompanyEvent> companyEvents,
                                                                HttpServletRequest httpServletRequest) throws ArgumentErrorException, IOException {
        Long userId = headerUtil.getHeaderUser();
        if(userId == null){
            throw new ArgumentErrorException("未获取到登录信息，请重新登录！");
        }
        String ip = headerUtil.getIp();
        String ipAddress = IpAddressUtil.getIpAddress(ip);
        exposureService.exposureBatch(companyEvents,userId,ip,ipAddress);
        return new ResponseEntity<>(new ResultObject<>("success"), HttpStatus.OK);
    }

    @ApiOperation("发送单击企业")
    @PostMapping("/sendClickCompany")
    @OperationRecord(menu = "企业事件库管理", actionType = SystemConsts.ACTION.ADD)
    public ResponseEntity<ResultObject> sendClickCompany(@RequestBody CompanyEvent companyEvents,
                                                         HttpServletRequest httpServletRequest) throws ArgumentErrorException, IOException {
        Long userId = headerUtil.getHeaderUser();
        if(userId == null){
            throw new ArgumentErrorException("未获取到登录信息，请重新登录！");
        }
        String ip = headerUtil.getIp();
        String ipAddress = IpAddressUtil.getIpAddress(ip);
        exposureService.click(companyEvents,userId,ip,ipAddress);
        return new ResponseEntity<>(new ResultObject<>("success"), HttpStatus.OK);
    }
}
